package examples;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;

import com.hyperrealm.kiwi.ui.*;
import com.hyperrealm.kiwi.ui.model.*;
import com.hyperrealm.kiwi.util.*;
import com.hyperrealm.kiwi.ui.dialog.*;

public class ListModelTest extends KFrame implements ActionListener
{
  private DefaultKListModel<MyColor> model;
  private KButton b_add, b_remove, b_change;
  private KListModelListAdapter listAdapter;
  private KListModelComboBoxAdapter cboxAdapter;
  private KListModelTableAdapter tableAdapter;
  private Color colors[] = new Color[] { Color.red, Color.green, Color.blue,
                                         Color.yellow, Color.black,
                                         Color.orange, Color.cyan,
                                         Color.pink };
  private String colorNames[] = new String[] { "Red", "Green", "Blue",
                                               "Yellow", "Black", "Orange",
                                               "Cyan", "Pink" };
  private JList list;
  private JComboBox cbox;
  private KTable table;

  public static void main(String args[])
  {
    ListModelTest lmt = new ListModelTest();
    lmt.setVisible(true);
  }

  public ListModelTest()
  {
    super("ListModelTest");

    model = new MyListModel();

    KPanel p = getMainContainer();
    p.setBorder(KiwiUtils.defaultBorder);

    p.setLayout(new BorderLayout(5, 5));

    cbox = new JComboBox();
    cboxAdapter = new KListModelComboBoxAdapter(cbox);
    cboxAdapter.setListModel(model);

    cbox.setModel(cboxAdapter);

    p.add("North", cbox);

    KPanel mid = new KPanel();
    mid.setLayout(new GridLayout(0, 1, 5, 5));

    p.add("Center", mid);

    Dimension sz = new Dimension(200, 200);
    
    list = new JList();
    KScrollPane sp = new KScrollPane(list);
    listAdapter = new KListModelListAdapter(list);
    listAdapter.setListModel(model);

    list.setModel(listAdapter);

    sp.setSize(sz);
    sp.setPreferredSize(sz);
    sp.setMinimumSize(sz);
    mid.add(sp);

    table = new KTable();
    table.setAutoCreateColumnsFromModel(true);
    sp = new KScrollPane(table);
    tableAdapter = new KListModelTableAdapter();
    tableAdapter.setListModel(model);

    table.setModel(tableAdapter);

    sp.setSize(sz);
    sp.setPreferredSize(sz);
    sp.setMinimumSize(sz);
    mid.add(sp);
    
    ButtonPanel bp = new ButtonPanel();

    b_add = new KButton("Add Item");
    b_add.addActionListener(this);
    bp.addButton(b_add);

    b_remove = new KButton("Remove Item");
    b_remove.addActionListener(this);
    bp.addButton(b_remove);
    
    b_change = new KButton("Change Item");
    b_change.addActionListener(this);
    bp.addButton(b_change);

    p.add("South", bp);

    for(int i = 0; i < colors.length; i++)
      model.addItem(new MyColor(i));

    pack();
  }

  public void actionPerformed(ActionEvent evt)
  {
    Object o = evt.getSource();

    if(o == b_add)
    {
      int x = KiwiUtils.randomInt(colors.length);

      model.addItem(new MyColor(x));
    }

    else if(o == b_remove)
    {
      MyColor myc = (MyColor)list.getSelectedValue();
      if(myc != null)
      {
        model.removeItem(myc);
      }
    }

    else if(o == b_change)
    {
      MyColor myc = (MyColor)list.getSelectedValue();
      if(myc != null)
      {
        int x = KiwiUtils.randomInt(colors.length);

        myc.setColor(x);
        model.updateItem(myc);
      }
    }
  }

  /*
   */
  
  class MyColor
  {
    Color color;
    String name;
    Icon icon;

    MyColor(int i)
    {
      setColor(i);
    }

    void setColor(int i)
    {
      color = colors[i];
      name = colorNames[i];

      icon = new ColorSwatch(color, 16, 16);
    }    
  }

  /*
   */
  
  class MyListModel extends DefaultKListModel<MyColor>
  {
    private final String columns[]
      = new String[] { "Name", "Red", "Green", "Blue" };
    private final Class types[]
      = new Class[] { String.class, Integer.class, Integer.class,
                      Integer.class };
    
    public String getLabel(MyColor item)
    {
      return(item.name);
    }

    public Icon getIcon(MyColor item)
    {
      return(item.icon);
      
    }

    public int getFieldCount()
    {
      return(columns.length);
    }

    public String getFieldLabel(int field)
    {
      return(columns[field]);
    }

    public Class getFieldType(int field)
    {
      return(types[field]);
    }

    public Object getField(MyColor item, int field)
    {
      switch(field)
      {
        case 0:
          return(item.name);
        case 1:
          return(item.color.getRed());
        case 2:
          return(item.color.getGreen());
        case 3:
          return(item.color.getBlue());
        default:
          return(null);
      }
    }
    
  }
  
}
